<table width="100%" border="0" cellpadding="3" cellspacing="1"> 
    <tr bgcolor="#FDFDFD"> 
       <td width="20%"><strong>Dia</strong></td> 
       <td width="80%">         <table width="100%"  border="0" cellspacing="2" cellpadding="1">
           <tr bgcolor="#FBF7D9">
             <td bgcolor="#FFFFFF"><?php
				$TimeOutput = split (" ",$CallTime[1]['time_main']);
				print $TimeOutput[0];
           echo '<br>';
			echo '<b>Hora Actual:</b> <span id="servertime"></span>';?>
</td>
             <td width="25%" bgcolor="#FBF7D9"><div align="center">Total de Horário:<br>
               <?php
		$SchedTotal = 

$TimeSetts[1]['leavetime']-$TimeSetts[1]['entrytime']+$TimeSetts[1]['lunchstart']-$TimeSetts[1]['lunchend'];
		$Lunch = strtotime($TimeSetts[1]['lunchend'])-strtotime($TimeSetts[1]['lunchstart']);
		echo $SchedTotal; ?>
Horas             </div></td>
             <td width="25%" bgcolor="#FBF7D9"><div align="center">Toler&acirc;ncia:<br>
               <?php echo $Delay;?>
             </div></td>
             <td width="25%" bgcolor="#FBF7D9"><div align="center">AlmoÇo:
                 <br>
                 <?php echo date("H:i",$Lunch-3600);?>
             </div></td>
           </tr>
         </table></td> 
  </tr> 
    <tr bgcolor="#EEF0F0"> 
       <td width="20%" valign="top">Hora de Entrada:</td> 
       <td width="80%" bgcolor="#F9F9F9"> <?php
				$TimeOutput = split (" ",$CallTime[1]['InTime']);
				print $TimeOutput[1];?></td> 
     </tr> 
    <tr bgcolor="#EEF0F0"> 
       <td valign="top">Atraso:</td> 
       <td bgcolor="#F9F9F9"> <?php 

	if (isset($_POST['action']) && $_POST['action'] == 'EndDay') {
	
			$TimeVerify = split(":",$TimeSetts[1]['leavetime']);
			$TimeVerify = mktime($TimeVerify[0],$TimeVerify[1],$TimeVerify[2],0,0,0);
	
			$CurrentTimeEnd = split(" ",$CurrentTime);
			$CurrentVerify = split(":",$CurrentTimeEnd[1]);
			$CurrentVerify = mktime($CurrentVerify[0],$CurrentVerify[1],$CurrentVerify[2],0,0,0);
			
			if ($CurrentVerify > ($TimeVerify+15*60))  {
			
					$EventTime = $CurrentVerify-$TimeVerify+82800;
					$QEvent = "INSERT INTO timeclock_xtime (RefUser, Time, Status, Justification, EventTime, RefOperation) 
					VALUES 
					({$_SESSION['UtID']},'{$CurrentTime}',2,'{$_POST['Justification']}','{$EventTime}',{$CallTime[1]['ref']})";
					$Event = $GLOBALS['CONN']->db_query($QEvent); 
			}
	}

			$TimeVerify = split(":",$TimeSetts[1]['entrytime']);
			$TimeVerify = mktime($TimeVerify[0],$TimeVerify[1],$TimeVerify[2],0,0,0);
			
			$EntryVerify = split(":",$TimeOutput[1]);
			$EntryVerify = mktime($EntryVerify[0],$EntryVerify[1],$EntryVerify[2],0,0,0);
			
			$DelayTime = $EntryVerify-$TimeVerify;
			
			$DelayVerify = $DelayTime - $Tolerance;
				
			if ($DelayVerify > 0) {
			
				if ($DelayTime < $AdminTimeSetts[1]['delaylevel1']) {
				$DelayLevel = 1;
				} elseif ($DelayTime < $AdminTimeSetts[1]['delaylevel2']) {
				$DelayLevel = 2;
				} elseif ($DelayTime < $AdminTimeSetts[1]['delaylevel3']) {
				$DelayLevel = 3;
				} else {
				$DelayLevel = 4;
				}
					
					$QJustifications = "SELECT * FROM timeclock_delays 
					WHERE RefOperation = {$CallTime[1]['ref']} AND RefType = 1";
					$VJustifications = $GLOBALS['CONN']->db_array($QJustifications); 
					if(!isset($VJustifications[1])) {
					$QJus = "INSERT INTO timeclock_delays 
					(main_user_ref, delaylevel, delaytime, EventTime, Status, RefType,RefOperation) 
					VALUES 
					({$_SESSION['UtID']},{$DelayLevel},'{$DelayTime}','{$CurrentTime}',1,1,{$CallTime[1]['ref']})";
					$Jus = $GLOBALS['CONN']->db_query($QJus); 
					$Justifications = $GLOBALS['CONN']->db_array($QJustifications); 
					}
					elseif(isset($VJustifications[1]) && $VJustifications[1]['delaylevel'] == 0) {
					$QJus = "UPDATE timeclock_delays SET delaylevel = {$DelayLevel},
					delaytime = '{$DelayTime}',
					EventTime = '{$CurrentTime}' 
					WHERE RefOperation = {$CallTime[1]['ref']} AND RefType = 1 AND Status = 1";
					$Jus = $GLOBALS['CONN']->db_query($QJus); 
					$Justifications = $GLOBALS['CONN']->db_array($QJustifications); 
					} 
					if(isset($Justifications[1])) {
				echo '<form accept-charset="utf-8" action="?" method="post">
						<input type="hidden" name="ref" value="'.$Justifications[1]['ref'].'">
						<p>Tempo do Atraso: '.date("H:i:s",$DelayTime+82800).'
						<p>Grau do Atraso: '.$Justifications[1]['delaylevel'].'
				<p>Justifica��o<br>
				<select name="justification" id="justification">';
				$CMotivs = "SELECT * FROM htck_delaytypes";
				$Motivs = $GLOBALS['CONN']->db_array($CMotivs);
				for ($r = 1; $r <= $Motivs[0]; $r++) {
				echo '<option value="'.$Motivs[$r]['ref'].'">'.$Motivs[$r]['name'].'</option>';
				}
				echo '</select>
				<p>Descri��o <br>';
				mn_field('textarea','ED','Description','','40','6','False','');
				echo '<p><input type="submit" name="SubmitDelay" value="Concluir">
				</form>';
				} else {
				$QJustifications = "SELECT 
											a.delaylevel,
											b.name AS Justify,
											c.name AS Status
					FROM timeclock_delays AS a
					INNER JOIN htck_status AS c ON c.StatRef = a.Status
					LEFT OUTER JOIN htck_delaytypes AS b ON b.ref = a.Justify
					WHERE a.RefOperation = {$CallTime[1]['ref']}
					AND RefType = 1
					AND a.Status != 1";
				$Justifications = $GLOBALS['CONN']->db_array($QJustifications); 
				if(isset($Justifications[1])) {
				echo '<p>Tempo do Atraso: '.date("H:i:s",$DelayTime+82800).'
					<br>Grau do Atraso: '.$Justifications[1]['delaylevel'].'
					<br>Justifica��o: '.$Justifications[1]['Justify'].'
					<br>Estado: '.$Justifications[1]['Status'];
					}
				}
			} else {
			print "<span style=\"color:#00CC00\"><b>Sem Atraso</b></span>";
			} ?></td> 
     </tr> 
    <tr bgcolor="#EEF0F0"> 
       <td width="20%" valign="top">Hora de Saída</td> 
       <td width="80%" bgcolor="#F9F9F9">
	   <?php
	  	$CTime = split(" ",$CurrentTime);
		$CTim = strtotime($CTime[1]);
		$LTime = strtotime($TimeSetts[1]['leavetime'])+15*60;
	    if ($CallTime[1]['OutTime'] != "") {
				$TimeOutput = split (" ",$CallTime[1]['OutTime']);
				echo $TimeOutput[1];
		} elseif ($LTime > $CTim) {
		if (!isset($_POST['action'])) {
		$_POST['action'] = 'EndDayT'; 
		}?>
		<form action="<?php echo $GLOBALS['URL'];?>Modules/TimeClock/Front/index.php?action=<?php echo $_POST['action'];?>" method="post" name="UpdateDay">
		<input name="action" value="EndDay" type="hidden">
       <input name="Justification" type="hidden" cols="45" rows="2" id="Justification"></textarea>
       <br>
	  <input name="OutTime" type="hidden" value="<?php echo $CallTime[1]['OutTime'];?>">
    
	  <?php if (!isset($_POST['Day'])) {
	  mn_field('submit','ED','Day','Terminar Dia','','','','');
	  } elseif (isset($_POST['Day']) && !isset($_POST['Description']) && ($CallTime[1]['OutTime']-600) < $CurrentTime) {
	   	echo 'Vai terminar o dia antes do hor�rio, indique o motivo:<br>';
		mn_field('textarea','ED','DescriptionOUT','','45','2','False','').'<br>';
		mn_field('submit','ED','Day','Confirmar sa�da','','','','');
		echo '<br>';
		mn_field('hidden','ED','action','EndDay','','','','');
		
	   } elseif (($CallTime[1]['OutTime']-600) >= $CurrentTime) {
	   mn_field('hidden','ED','DescriptionOUT','','45','2','False','');
	   mn_field('submit','ED','Day','Confirmar sa�da','','','','');
	   mn_field('hidden','ED','action','EndDay','','','','');
	   } ?>
	    </form>
	   <?php } else {
	   echo '<form accept-charset="utf-8" action="'.$GLOBALS['URL'].'Modules/TimeClock/Front/index.php?action=EndDay" method="post" name="UpdateDay">
	   <input name="action" value="EndDay" type="hidden">
	   <input name="Day" type="submit" id="Day" value="Terminar Dia">
	   <input type="hidden" name="Justification" value="">
	   </form>';
	   } ?>
</td> 
	</tr>  
    <tr bgcolor="#EEF0F0"> 
       <td valign="top">Saída para AlmoÇo </td> 
       <td bgcolor="#F9F9F9">
	   <?php			
				if ($CallTime[1]['LunchStart'] != "") {
				$TimeOutput = split (" ",$CallTime[1]['LunchStart']);
				echo $TimeOutput[1];
				} ?></td> 
     </tr> 
    <tr bgcolor="#EEF0F0"> 
       <td valign="top">Regresso do AlmoÇo </td> 
       <td bgcolor="#F9F9F9">
	   <?php			
				if (isset($_GET['LunchEnd']) && strtotime($CallTime[1]['LunchEnd'])-strtotime($CallTime[1]['LunchStart']) > 4200) {
	 	  		mn_field('textarea','ED','Description','','45','2','False','');
	   			mn_field('submit','ED','LunchDescription','Fechar','','','','');
				}
				if ($CallTime[1]['LunchEnd'] != "") {
				$TimeOutput = split (" ",$CallTime[1]['LunchEnd']);
				echo $TimeOutput[1];
				}?></td> 
     </tr> 
    <tr bgcolor="#EEF0F0"> 
       <td width="20%" valign="top"><br>
       Sa�das</td> 
       <td width="80%" bgcolor="#F9F9F9">
	   <?php  
	   			if ($CallTime[1]['Breaks'] != "") {
	
		$COff = "SELECT a.delaytime, 
						c.name AS Justify,
						b.name AS Status 
						FROM timeclock_delays AS a 
						INNER JOIN htck_status AS b ON b.StatRef = a.Status AND b.Type = 1
						INNER JOIN htck_delaytypes AS c ON c.ref = a.Justify
						WHERE a.main_user_ref = {$_SESSION['UtID']}
					AND DATE(a.EventTime) = DATE('$CurrentTime')
					AND a.RefType = 3
					AND a.Status = 2";
		$Off = $GLOBALS['CONN']->db_array($COff);
		for ($r = 1; $r <= $Off[0]; $r++) {
		
			echo '<br>'.date("H:i:s",$Off[$r]['delaytime']-3600).' '.$Off[$r]['Justify'].' '.$Off[$r]['Status'];
		}
	
		$TimeBreaks = split(";",$CallTime[1]['Breaks']);
		$CBreaks = count ($TimeBreaks);
		$parImpar = ($CBreaks % 2 == 1) ? 0 : 1;
		if ($parImpar == 1)  {
			echo '<p>Ausente de momento
			<form action="'.$GLOBALS['URL'].'Modules/TimeClock/Front/index.php?" method="post">
				<input type="hidden" name="TimeOff" value="'.($CBreaks-2).'">
				<input type="hidden" name="refOp" value="'.$CallTime[1]['ref'].'">
				<input type="hidden" name="OpOff" value="'.$CallTime[1]['Breaks'].'">
				<p>Justifica��o<br>
				<select name="justification" id="justification">';
				$CMotivs = "SELECT * FROM htck_delaytypes";
				$Motivs = $GLOBALS['CONN']->db_array($CMotivs);
				for ($r = 1; $r <= $Motivs[0]; $r++) {
				echo '<option value="'.$Motivs[$r]['ref'].'">'.$Motivs[$r]['name'].'</option>';
				}
				echo '</select>
				<p><input type="submit" name="SubmitOff" value="Voltar">
			</form>';
		}
	} ?> </td> 
  </tr> 
  </table>